package com.ds.infrastructure.hyperspace.console.dto.configdata;

import com.google.common.base.Preconditions;
import com.ds.infrastructure.hyperspace.console.enums.ContainerConfigEnum;
import com.ds.infrastructure.hyperspace.console.exceptions.HyperspaceException;
import com.ds.infrastructure.hyperspace.console.utils.FastJsonUtil;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.io.Serializable;
import java.util.List;

/**
 * @author: yaozhendong
 * @create: 2019-09-20 16:10
 **/
@Data
@NoArgsConstructor
public class ConfigDataDTO implements Serializable {
    //
    private Integer proxyId;
    //
    private String proxyName;
    private List<ResourcesDTO> resources;
    private List<ProtocolsDTO> protocols;
    private List<PipelinesDTO> pipelines;
    private List<EndpointsDTO> endpoints;

    public static String getNamespace(Long environmentId, Long stageId, String type) {
        return environmentId + "_" + stageId + "_" + type;
    }

    public static String getNamespace(String name, String type) {
        return name + "_" + type;
    }

    public enum ENDPOINT_TYPE {
        TARGET("TARGET"), ENTRY("ENTRY");
        public final String value;

        ENDPOINT_TYPE(String value) {
            this.value = value;
        }
    }

    public static String merge(ContainerConfigEnum.CONFIG_DATA_UPDATE_TYPE updateType, String oldConfigDataJson, String newConfigDataJson) {
        ConfigDataDTO oldConfigData = FastJsonUtil.deserialize(oldConfigDataJson, ConfigDataDTO.class);
        ConfigDataDTO newConfigData = FastJsonUtil.deserialize(newConfigDataJson, ConfigDataDTO.class);
        if (ContainerConfigEnum.CONFIG_DATA_UPDATE_TYPE.BASE_DATA.equals(updateType)) {
            oldConfigData.setResources(newConfigData.getResources());
            oldConfigData.setEndpoints(newConfigData.getEndpoints());
            oldConfigData.setProtocols(newConfigData.getProtocols());
            return FastJsonUtil.serialize(oldConfigData);
        }
        if (ContainerConfigEnum.CONFIG_DATA_UPDATE_TYPE.PIPELINES_DATA.equals(updateType)) {
            oldConfigData.setPipelines(newConfigData.getPipelines());
            return FastJsonUtil.serialize(oldConfigData);
        }
        throw new HyperspaceException("没有找到对应的数据修改类型，当前updateType=" + updateType);
    }

    public static void checkConfigDataDTO(String configDataJson) {
        ConfigDataDTO configDataDTO = FastJsonUtil.deserialize(configDataJson, ConfigDataDTO.class);
        try {
            Preconditions.checkArgument(configDataDTO != null, "configDataDTO=null");
            Preconditions.checkArgument(configDataDTO.getResources() != null, "缺少Resources的配置信息，configDataDTO.getResources()=null");
            Preconditions.checkArgument(configDataDTO.getProtocols() != null, "缺少Protocols的配置信息，configDataDTO.getProtocols()=null");
            Preconditions.checkArgument(configDataDTO.getEndpoints() != null, "缺少Endpoints的配置信息，configDataDTO.getEndpoints()=null");
            Preconditions.checkArgument(configDataDTO.getPipelines() != null, "缺少Pipelines的配置信息，configDataDTO.getPipelines()=null");
        } catch (IllegalArgumentException ex) {
            throw new HyperspaceException(ex.getMessage());
        }
    }

    public static void checkConfigDataDTOForApiRouter(ConfigDataDTO configDataDTO) {
        try {
            Preconditions.checkArgument(configDataDTO != null, "configDataDTO=null");
            Preconditions.checkArgument(configDataDTO.getEndpoints() != null, "缺少Endpoints的配置信息，configDataDTO.getEndpoints()=null");
            Preconditions.checkArgument(configDataDTO.getPipelines() != null, "缺少Pipelines的配置信息，configDataDTO.getPipelines()=null");
        } catch (IllegalArgumentException ex) {
            throw new HyperspaceException(ex.getMessage());
        }
    }

}
